package com.therandoms.rtgdroid.steps;

import java.util.ArrayList;

import org.codepond.wizardroid.WizardStep;
import org.codepond.wizardroid.persistence.ContextVariable;
import org.therandoms.textgen.TextGeneratorDictionary;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

import com.mobeta.android.dslv.DragSortController;
import com.mobeta.android.dslv.DragSortListView;
import com.therandoms.rtgdroid.R;

/**
 * Prompts the user for the production categories to start at when making random text. 
 * @author Logan Widick
 * @author Vinh Pham
 */
@SuppressLint("ClickableViewAccessibility")
public class ProductionCategorySelectionStep extends WizardStep {

	/**
	 * The grammar object itself
	 */
	@ContextVariable
	private TextGeneratorDictionary<String> grammar;

	/**
	 * The list of desired production categories
	 */
	@ContextVariable
	private ArrayList<String> desiredProductionCategories;

	/**
	 * The spinner for production categories in the grammar.
	 */
	private Spinner productionCategoriesSpinner = null;

	/**
	 * Stores the production categories.
	 */
	private DragSortListView productionCategoriesListView = null;

	/**
	 * The adapter for the drag sort list view
	 */
	private ArrayAdapter<String> adapter = null;

	/**
	 * The drop listener for the drag sort list view
	 */
	private DragSortListView.DropListener onDrop = new DragSortListView.DropListener() {
		@Override
		public void drop(int from, int to) {
			if (from != to) {
				String item = adapter.getItem(from);
				adapter.remove(item);
				adapter.insert(item, to);
			}
		}
	};

	/**
	 * The remove listener for the drag sort list view
	 */
	private DragSortListView.RemoveListener onRemove = new DragSortListView.RemoveListener() {
		@Override
		public void remove(int which) {
			// TODO Auto-generated method stub
			adapter.remove(adapter.getItem(which));

			// inform the wizard that we have NOT completed.
			if (adapter.getCount() < 0) {
				getActivity().runOnUiThread(new Runnable() {

					@Override
					public void run() {
						notifyCompleted(false);
					}
				});
			}
		}
	};
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setHasOptionsMenu(true);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		//false is really important here. The program will crash without it. 
		View v = inflater.inflate(R.layout.step_select_production_categories,
				container, false);
		
		//get production categories
		productionCategoriesSpinner = (Spinner) v
				.findViewById(R.id.selectProductionCategorySpinner);
		productionCategoriesSpinner.setAdapter(new ArrayAdapter<String>(
				getActivity(), android.R.layout.simple_spinner_item,
				new ArrayList<String>(grammar.getProductionCategories())));

		// get dslv and adapter
		productionCategoriesListView = (DragSortListView) v
				.findViewById(R.id.productionCategoryDragSortView);
		adapter = new ArrayAdapter<String>(getActivity(),
				android.R.layout.simple_list_item_1);
		productionCategoriesListView.setAdapter(adapter);

		// Set the listeners for the DSLV
		productionCategoriesListView.setDropListener(onDrop);
		productionCategoriesListView.setRemoveListener(onRemove);

		// Add a controller to the DSLV
		DragSortController controller = new DragSortController(
				productionCategoriesListView);
		controller.setRemoveEnabled(true);
		controller.setSortEnabled(true);
		controller.setDragInitMode(1);
		productionCategoriesListView.setFloatViewManager(controller);
		productionCategoriesListView.setOnTouchListener(controller);
		productionCategoriesListView.setDragEnabled(true);

		// set the button onclick event for the add button
		Button b = (Button) v.findViewById(R.id.productionCategoryAddButton);
		b.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				//add the currently selected production category to the list view. 
				adapter.add(productionCategoriesSpinner.getSelectedItem()
						.toString());

				// this says we are free to exit; it DOES NOT automatically exit
				notifyCompleted(true);
			}
		});
		
		return v;
	}

	@Override
	public void onExit(int exitCode) {
		//xfer all selected production categories to the arraylist. 
		desiredProductionCategories.clear();
		for (int i = 0; i < adapter.getCount(); i++) {
			desiredProductionCategories.add(adapter.getItem(i));
		}
		super.onExit(exitCode);
	}
}
